<html><head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"><title>CreateSemaphore</title></head>
<body bgcolor="#EFF1F0" link="#3A3966" vlink="#000000" alink="#000000">
<font face="Verdana, sans-serif" size="2"><p align="center"><b><font size="4">CreateSemaphore()</font></b></p>
<p><b>语法</b></p><blockquote>

Semaphore = <font color="#3A3966"><b>CreateSemaphore</b></font>([InitialCount])</blockquote>

</blockquote>
<b>概要</b><br><blockquote>



Creates a new semaphore object. 
<br>
<br>
A semaphore is a thread synchronization object that keeps an internal count. It has two kinds of operations: 
<a href="signalsemaphore.html">signal</a> and <a href="waitsemaphore.html">wait</a>. A wait operation decreases the count 
of the semaphore by one. If the count would drop below 0, the wait operation will block until a signal call is made. 
A signal operation increases the count one, releasing a blocking thread if there is one. A semaphore allows 
to enforce minimum or maximum counts across threads for example to prevent a queue from running out or getting 
too many items. 
<br>
<br>
Unlike a <a href="createmutex.html">mutex</a>, a semaphore object is not "owned" by a particular thread, which means that 
signal/wait calls do not need to be done from the same thread as it is the case with <a href="lockmutex.html">LockMutex()</a> and 
<a href="unlockmutex.html">UnlockMutex()</a>. In fact a common use for a semaphore objects is for one thread to do the 
<a href="signalsemaphore.html">SignalSemaphore()</a> calls and for another one to do all <a href="waitsemaphore.html">WaitSemaphore()</a> calls to implement 
a producer/consumer pattern. 

 

</blockquote><p><b>参数</b></p><blockquote>
<style type="text/css">
table.parameters { border-spacing: 0px; border-style: none; border-collapse: collapse; }
table.parameters td { border-width: 1px; padding: 6px; border-style: solid; border-color: gray; vertical-align: top; font-family:Arial; font-size:10pt; }
</style>
<table width="90%" class="parameters">
<tr><td width="10%"><i>InitialCount (optional)</i></td>
<td width="90%"> 
 
It has be a positive value that specifies the initial count of the semaphore. 
If it is not specified, the initial count is zero. 

 

</td></tr>
</table>
</blockquote><p><b>返回值</b></p><blockquote>



The new semaphore, or zero if the semaphore creation has failed. 

 

</blockquote><p><b>示例</b></p><blockquote>



This example shows a "producer" thread populating a queue with elements and the main thread reading them. 
The semaphore is used to make sure the queue never runs out of elements. Note that this could also be 
achieved with only a mutex and waiting/polling for queue elements with a <a href="../system/delay.html">Delay()</a> in between, 
but the semaphore commands do a more efficient wait (returning immediately at the <a href="signalsemaphore.html">SignalSemaphore()</a> call 
and not just on the next time a polling loop would check the queue). 
 
<pre><font face="Courier New, Courier, mono"size="2"><b><font color="#3A3966">Global</font></b> Semaphore =<font color="#3A3966"> CreateSemaphore</font>()
<b><font color="#3A3966">Global</font></b> Mutex     =<font color="#3A3966"> CreateMutex</font>()
<b><font color="#3A3966">Global</font></b> <b><font color="#3A3966">NewList</font></b> <font color="#3A3966">Queue</font>()

<b><font color="#3A3966">Procedure</font></b> <font color="#3A3966">Producer</font>(Total)

  <b><font color="#3A3966">For</font></b> i = 1 <b><font color="#3A3966">To</font></b> Total
<font color="#3A3966">    Delay</font>(<font color="#3A3966">Random</font>(750) + 250)
    
    <font color="#3A3966">; The queue access still needs a normal mutex lock to be thread-safe</font>
<font color="#3A3966">    LockMutex</font>(Mutex)
<font color="#3A3966">      LastElement</font>(<font color="#3A3966">Queue</font>())
<font color="#3A3966">      AddElement</font>(<font color="#3A3966">Queue</font>())
<font color="#3A3966">      Queue</font>() = i
<font color="#3A3966">    UnlockMutex</font>(Mutex)

    <font color="#3A3966">; Signal that there is a new queue element</font>
<font color="#3A3966">    SignalSemaphore</font>(Semaphore)
  <b><font color="#3A3966">Next</font></b> i
    
<b><font color="#3A3966">EndProcedure</font></b>

<b><font color="#3A3966">If</font></b> <font color="#3A3966">CreateThread</font>(@Producer(), 30)

  <b><font color="#3A3966">For</font></b> i = 1 <b><font color="#3A3966">To</font></b> 30
    <font color="#3A3966">; wait for one element to be available</font>
<font color="#3A3966">    WaitSemaphore</font>(Semaphore)
    
    <font color="#3A3966">; display the queue state</font>
<font color="#3A3966">    LockMutex</font>(Mutex)
      Queue$ = "Queue:"
      <b><font color="#3A3966">ForEach</font></b> <font color="#3A3966">Queue</font>()
        Queue$ + " " +<font color="#3A3966"> Str</font>(<font color="#3A3966">Queue</font>())
      <b><font color="#3A3966">Next</font></b> <font color="#3A3966">Queue</font>()
      <b><font color="#3A3966">Debug</font></b> Queue$
    
      <font color="#3A3966">; remove head element from the queue</font>
<font color="#3A3966">      FirstElement</font>(<font color="#3A3966">Queue</font>())
<font color="#3A3966">      DeleteElement</font>(<font color="#3A3966">Queue</font>())
<font color="#3A3966">    UnlockMutex</font>(Mutex)
    
  <b><font color="#3A3966">Next</font></b> i

<b><font color="#3A3966">EndIf</font></b>
</font></pre>

</blockquote><p><b>参阅</b></p><blockquote>

<a href="freesemaphore.html">FreeSemaphore()</a> 

</Blockquote><p><b>已支持操作系统 </b><Blockquote>所有</Blockquote></p><center>&lt;- <a href=createmutex.html>CreateMutex()</a> - <a href="index.html">Thread Index</a> - <a href="createthread.html">CreateThread()</a> -&gt;<br><br>

</body></html>